package com.company.project.component.excel.demo;


import com.company.project.component.excel.export.ExportConfigFactory;
import com.company.project.component.excel.export.FileExportor;
import com.company.project.component.excel.export.domain.common.ExportConfig;
import com.company.project.component.excel.export.domain.common.ExportResult;
import com.company.project.component.excel.export.exception.FileExportException;
import com.company.project.component.excel.fade.ExcelUtil;
import com.company.project.component.excel.importfile.ConfigParser;
import com.company.project.component.excel.importfile.ConfigurationParserFactory;
import com.company.project.component.excel.importfile.FileImportExecutor;
import com.company.project.component.excel.importfile.domain.MapResult;
import com.company.project.component.excel.importfile.domain.common.Configuration;
import com.company.project.component.excel.importfile.exception.FileImportException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;

public class TestExcel {

    public static void main(String[] args)
    {
        try
        {
            testImport();
            testExport();
//            webtest();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void testImport() throws FileImportException, FileNotFoundException, URISyntaxException {

        ConfigParser configParser = ConfigurationParserFactory.getConfigParser(Configuration.ParserType.XML);
        URI uri = TestExcel.class.getResource("/excel/file/testImport.xlsx").toURI();

        File importFile = new File(uri);
        String configName = "config.xml";
        try
        {
            List<Map> maps = ExcelUtil.importExcelData(importFile , configName);
            for (Map<String, Object> map : maps)
            {
                int index = (int) map.get("index");
                float f1 = (float) map.get("float");
                String string = (String) map.get("string");
                Date date = (Date) map.get("date");
                BigDecimal bigDecimal = (BigDecimal) map.get("bigdecimal");
                System.out.println("index :" + index + " f1 : " + f1 + " string : " + string
                        + " date : " + date.toString() + " bigdecimal " + bigDecimal);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void testExport() throws Exception
    {
        String configName = "exportconfig.xml";
        //map也可以换成一个实体类
        List<Map> lists = new LinkedList<>();
        for (int i = 0; i < 10; i++)
        {
            Map<String, Object> maps = new HashMap<>();
            maps.put("index", i);
            maps.put("date", new Date());
            maps.put("greet", "hi" + i);
            maps.put("float", Float.valueOf(i));
            maps.put("bigdecimal", BigDecimal.valueOf(i));
            lists.add(maps);
        }
        OutputStream outputStream = new FileOutputStream("d://output.xlsx");
        ExcelUtil.exportExcelWithList(lists , configName , outputStream);

    }


//    @RequestMapping("/mytest1")
//    public void webtest(HttpServletRequest request , HttpServletResponse response) throws IOException, FileImportException, FileExportException {
//
////        Workbook workbook = ExcelUtil.getWorkbook(request);
////        ExcelUtil.readExcel(workbook);
//
//        List<Map> list = ExcelUtil.importExcelData(request , "config.xml");
//        System.out.println(JSON.toJSONString(list));
//
//        //map也可以换成一个实体类
//        List<Map> lists = new LinkedList<>();
//        for (int i = 0; i < 10; i++)
//        {
//            Map<String, Object> maps = new HashMap<>();
//            maps.put("index", i);
//            maps.put("date", DateUtil.yyyyMMddHHmm(new Date()));
//            maps.put("greet", "hi" + i);
//            maps.put("float", Float.valueOf(i));
//            maps.put("bigdecimal", BigDecimal.valueOf(i));
//            lists.add(maps);
//        }
//        ExcelUtil.exportExcelWithList(lists , "exportconfig.xml" , "abc" ,  response);
//
//    }

}
